<?php

namespace app\adminapi\controller;

use think\Controller;
use think\Request;

class Brand extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = input();
				$where = [];
				if(isset($params['cate_id']) && !empty($params['cate_id'])){
					//分类下的品牌列表
						$where['cate_id'] = $params['cate_id'];
						//查询数据
				  	$list = \app\common\model\Brand::where($where)->field('id,name')->select();
					// $list = \app\common\model\Brand::alias('t1')
					// ->join('pyg_category t2','t1.cate_id = t2.id','left')
					// ->field('t1.*,t2.cate_name')
					// ->where($where)->select();
				} else {
					//分页+搜索列表
						if(isset($params['keyword']) && !empty($params['keyword'])){
								$keyword = $params['keyword'];
								$where['t1.name'] = ['like', "%$keyword%"];
						}
						$list = \app\common\model\Brand::alias('t1')
					->join('pyg_category t2','t1.cate_id = t2.id','left')
					->field('t1.*,t2.cate_name')
					->where($where)->paginate(10);
				}
				$this->ok($list);
		
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //接收参数
				$params = input();
				//参数检测
				$validate = $this->validate($params,[
					'name'=> 'require',
					'cate_id' => 'require|integer|gt:0',
					'is_hot' => 'require|int:0,1',
					'sort' => 'require|between:0,9999'
				]);
				if($validate != true){
					$this->fail($validate);
				}
        //缩略图
        if(isset($params['logo']) && !empty($params['logo']) && is_file('.'. $params['logo'])){
            \think\Image::open('.'. $params['logo'])->thumb(200,100)->save('.'. $params['logo']);
        }
				//添加数据
				$brand = \app\common\model\Brand::create($params,true);
				$info = \app\common\model\Brand::find($brand['id']);
				//返回数据
				$this->ok($info);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $info = \app\common\model\Brand::find($id);
				$this->ok($info);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //接收参数
				$params = input();
				//参数检测
				$validate = $this->validate($params,[
					'name'=> 'require',
					'cate_id' => 'require|integer|gt:0',
					'is_hot' => 'require|int:0,1',
					'sort' => 'require|between:0,9999'
				]);
				if($validate != true){
					$this->fail($validate);
				}
        //缩略图
        if(isset($params['logo']) && !empty($params['logo']) && is_file('.'. $params['logo'])){
            \think\Image::open('.'. $params['logo'])->thumb(200,100)->save('.'. $params['logo']);
        }
				//添加数据
				$res = \app\common\model\Brand::update($params,['id',$id],true);
				if(empty($res)){
					$this->fail('修改失败');
				}
				$info = \app\common\model\Brand::find($id);
				$this->ok($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $total = \app\common\model\Goods::where('brand_id',$id)->count();
				if($total > 0){
						$this->fail('该品牌下有商品不能删除');
				}
				\app\common\model\Brand::destroy($id);
				$this->ok();
    }
}
